package br.urca.impl;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.DisciplinasMatriculadasDAO;
import br.urca.model.DisciplinaMatriculada;
import br.urca.model.Oferta;

@Transactional
public class JPADisciplinasMatriculadasDAO implements DisciplinasMatriculadasDAO {
	
	private EntityManager manager;
	
	public JPADisciplinasMatriculadasDAO() {
		this(null);
	}
	
	@Inject
	public JPADisciplinasMatriculadasDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<DisciplinaMatriculada> todos() {
		return this.manager.createQuery("SELECT d FROM DisciplinaMatriculada d", DisciplinaMatriculada.class)
				.getResultList();
	}

	@Override
	public List<DisciplinaMatriculada> buscarPorMatricula(String matricula) {
		return this.manager
				.createQuery("SELECT d FROM DisciplinaMatriculada d "
						+ "WHERE d.id.matricula = :matricula "
						+ "ORDER BY d.id.codOferta", 
						DisciplinaMatriculada.class).setParameter("matricula", matricula)
				.getResultList();		
	}

	@Override
	public List<DisciplinaMatriculada> buscarPorOferta(Oferta oferta) {
		return this.manager
				.createQuery("SELECT d FROM DisciplinaMatriculada d "
						+ "WHERE d.id.codOferta = :codOferta "
						+ "AND d.id.codPedido = :codPedido "
						+ "ORDER BY d.id.codOferta", 
						DisciplinaMatriculada.class)
						.setParameter("codOferta", oferta.getId().getCodOferta())
						.setParameter("codPedido", oferta.getId().getCodPedido())
				.getResultList();
	}

	@Override
	public void atualizar(DisciplinaMatriculada disciplinaMatriculada) {
			this.manager.merge(disciplinaMatriculada);
	}

	@Override
	public DisciplinaMatriculada buscarPorOfertaPedidoMatricula(String oferta,
			String pedido, String matricula) {
		return this.manager
				.createQuery("SELECT d FROM DisciplinaMatriculada d "
						+ "WHERE d.id.codOferta = :codOferta "
						+ "AND d.id.codPedido = :codPedido "
						+ "AND d.id.matricula = :matricula "
						+ "ORDER BY d.id.codOferta", 
						DisciplinaMatriculada.class)
						.setParameter("codOferta", oferta)
						.setParameter("codPedido", pedido)
						.setParameter("matricula", matricula)
				.getSingleResult();
	}
	
	@Override
	public List<DisciplinaMatriculada> getDiscParaHistorico(String matricula) {
		List<String> situacoesExcluidas = Arrays.asList("CD", "CH", "DP", "FP",
				"IN", "NA", "NE", "NS", "RE", "CI", "FV", "LI");
		try {
			return this.manager
					.createQuery(
							"SELECT d FROM DisciplinaMatriculada d "
									+ "WHERE d.id.matricula = :matricula "
									+ "AND d.situacao.codSituacao not in (:situacoes) "
									+ "ORDER BY d.id.codOferta ",
									DisciplinaMatriculada.class)
					.setParameter("matricula", matricula)
					.setParameter("situacoes", situacoesExcluidas)
					.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
